package com.zzx.schoolfellow.controller;

import com.zzx.schoolfellow.model.dto.ChatMessageDTO;
import com.zzx.schoolfellow.model.entity.ChatMessage;
import com.zzx.schoolfellow.model.entity.UmsUser;
import com.zzx.schoolfellow.service.ChatMessageService;
import com.zzx.schoolfellow.service.impl.ChatMessageServiceImpl;
import com.zzx.schoolfellow.service.impl.IUmsUserServiceImpl;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/messages")
public class ChatMessageController {

    private final ChatMessageServiceImpl chatMessageService;


    @Autowired
    public ChatMessageController(ChatMessageServiceImpl chatMessageService) {
        this.chatMessageService = chatMessageService;
    }
    // 发送消息
    @PostMapping("/send")
    public String sendMessage(@RequestBody ChatMessage message) {
        chatMessageService.sendMessage(message);
        return "success";
    }


    // 获取聊天记录
    @GetMapping("/list")
    public List<ChatMessage> getMessageList(Integer fromUser, Integer toUser) {
        return chatMessageService.getMessageList(fromUser, toUser);
    }
    @PostMapping("")
    public ChatMessage createMessage(@RequestBody ChatMessageDTO messageDTO) {
        return chatMessageService.createMessage(messageDTO);
    }

    @PostMapping("/{id}")
    public List<ChatMessage> getMessages(@PathVariable String id) {
        return chatMessageService.getMessage(id);
    }

    @GetMapping("/{username}/{authorName}")
    public List<ChatMessage> getMessage(@PathVariable String username,@PathVariable String authorName) {
        return chatMessageService.getMessage(username,authorName);
    }

    @DeleteMapping("/{id}")
    public void deleteMessage(@PathVariable String id) {
        chatMessageService.deleteMessage(id);
    }

    @GetMapping("/chatroom/{id}")
    public ChatMessage getChatRoomById(@PathVariable Long id) throws NotFoundException {
        ChatMessage chatRoom = chatMessageService.getById(id);
        if (chatRoom == null) {
            throw new NotFoundException("Chat room not found");
        }

        return chatRoom;
    }
}
